angular.module('BatchBind')
    .controller('BatchBind.BatchBindCtrl', ['$scope', '$state', '$http', '$ionicLoading', '$ionicPopup', 'Http', 'imanDialog', 'imanUsbPort', 'Device',
        function($scope, $state, $http, $ionicLoading, $ionicPopup, Http, imanDialog, imanUsbPort, Device) {
            $scope.data = {
                laserId: '',
                size: '3cm',
                Barcode: '',
                Molot: ''
            };

            var toast;

            $scope.showToast = function(styleMes) {
                if (toast) {
                    toast.hide();
                }
                toast = imanDialog.toast({
                    message: styleMes,
                    position: 'center',
                    duration: false,
                    backdropClickToClose: false,
                    hardwareBackButtonClose: false
                });
            };

            $scope.showToast('请扫描单品信息');

            $scope.hideToast = function() {
                if (toast) {
                    toast.hide();
                    toast = null;
                }
            };
            $scope.$on('$ionicView.beforeEnter', function() {
                imanUsbPort.start();
            });

            $scope.$on('imanUsbPort.input', function(e, inputStr) {
                $scope.data.Barcode = inputStr;
                getQcInfo($scope.data.Barcode);
            });

            $scope.$on('$ionicView.afterLeave', function() {
                imanUsbPort.stop();
                if (toast) {
                    toast.hide();
                    toast = null;
                }
            });

            $scope.focus = function() {
                imanUsbPort.stop();
            };

            $scope.blur = function() {
                imanUsbPort.start();
            };

            $scope.clearInput = function() {
                $scope.data.laserId = '';
            };

            $scope.submit = function() {
                if (!$scope.data.laserId || $scope.data.laserId === '') {
                    imanDialog.alert('请扫描镭射标', '信息不全');
                    return;
                }
                $ionicLoading.show();
                Http.post({
                    url: '/api/barcode/MOLotNumberToAssemblyBarcode',
                    data: {
                        Parameter: {
                            Barcode: $scope.data.Barcode,
                            SecurityId: $scope.data.laserId,
                            Molot: $scope.data.barcode.MOLotNumber
                        }
                    }
                }).then(function() {
                    $ionicLoading.hide();
                    imanDialog.alert('绑定成功', '提交成功');
                })['finally'](function() {
                    $ionicLoading.hide();
                });
            };

            function getQcInfo(Barcode) {
                $ionicLoading.show();
                Http.post({
                    url: '/api/Wrapper/getWrapperInfo',
                    data: {
                        Parameter: {
                            Barcode: Barcode,
                            TerminalUniqueID: Device.getUUID()
                        }
                    }
                }).then(function(info) {
                    if (info.basicInfo.MOCode) {
                        $scope.hideToast();
                    }
                    $scope.data.barcode = info.basicInfo;
                    $scope.data.qcStandard = info.qcStandard;
                    $scope.data.Users = info.basicInfo.Users;
                    $scope.data.Molot = $scope.data.barcode.MOLotNumber;
                })['finally'](function() {
                    $ionicLoading.hide();
                });
            }

            //选择尺寸信息
            $scope.showSelectDialog = function() {
                imanDialog.showList({
                    title: '请选尺寸',
                    list: [
                        { id: 0, name: '1cm' },
                        { id: 1, name: '2cm' },
                        { id: 2, name: '3cm' },
                        { id: 3, name: '4cm' },
                        { id: 4, name: '5cm' },
                        { id: 5, name: '6cm' },
                        { id: 6, name: '7cm' },
                        { id: 7, name: '8cm' },
                        { id: 8, name: '9cm' },
                        { id: 9, name: '10cm' }
                    ],
                    valueField: 'id',
                    displayField: 'name',
                    value: $scope.data.ListId ? $scope.data.ListId : 2
                }).then(function(select) {
                    $scope.data.size = select.name;
                    $scope.data.ListId = select.id;
                });
            };
        }
    ]);
